package com.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;

import com.bean.Client;
import com.bean.Housepayment;
import com.bean.Indent;
import com.bean.Marketing;
import com.bean.Room;
import com.bean.User;
import com.service.ClientService;
import com.service.HousepaymentService;
import com.service.IndentService;
import com.service.MarketingService;
import com.service.RoomService;
import com.service.UserService;
import com.util.Export;
import com.util.Export1;

import duanxin.DingShiQi_01;



@Controller
@RequestMapping("/indent")
public class IndentController {

	@Autowired
	private IndentService service;
	
	@Autowired
	private UserService us;
	
	
	@Autowired
	private MarketingService ms;
	
	
	@Autowired
	private RoomService rs;
	
	
	@Autowired
	private IndentService is;
	
	@Autowired
	private HousepaymentService hs;
	
	@Autowired
	private ClientService cs;
	
	

	@RequestMapping("/indent.do")
    public String indent(HttpServletRequest rs,Model model){
		Integer uid = (Integer) rs.getSession().getAttribute("uid");
		Integer rid = (Integer) rs.getSession().getAttribute("rid");
		List<Indent> list=new ArrayList<Indent>();
		
		if(rid==1){
			
			list=service.selectAllByUserid(uid);
	       
		}
		if(rid==2)
		{
			list=service.selectAll();
			
		}
		
	model.addAttribute("indentList",list);
	  	return "indent/Indentlist";
    }
	
	
	@RequestMapping("/select.do")
    public String select(String uname,String cname,HttpServletRequest rs,Model model){
		Integer uid = (Integer) rs.getSession().getAttribute("uid");
		Integer rid = (Integer) rs.getSession().getAttribute("rid");
		
		
		List<Indent> list=new ArrayList<Indent>();
		
		if(rid==1){
			
			Map<String,Object> map=new HashMap<String, Object>();
			map.put("uname", uname);
			map.put("cname", cname);
			map.put("userid", uid);
			
			list=service.selectAllBycnameuname(map);
	       
		}
		if(rid==2)
		{
			Map<String,Object> map1=new HashMap<String, Object>();
			map1.put("uname", uname);
			map1.put("cname", cname);
			list=service.selectAllcnameuname(map1);
			
		}
		
	model.addAttribute("indentList",list);
	System.out.println("kjdhkhkdhj="+list.size());
	  	return "indent/Indentlist";
    }
	
	
	
	

	@RequestMapping("/add.do")
    public String add(Integer ace,Model model){
		Date date=new Date();
		Marketing marketing = ms.selectByPrimaryKey(ace);
		
		SimpleDateFormat formdate = new SimpleDateFormat("yyyy-MM-dd");
		
		Integer roomid = marketing.getRoomid();
		Room room = rs.selectByPrimaryKey(roomid);
		
		Integer userid = marketing.getUserid();
		User user = us.selectByPrimaryKey(userid);
		
		Integer cid = marketing.getClientid();
	     Client client = cs.selectOne(cid);
		
		
	     model.addAttribute("client",client);
		model.addAttribute("user",user);
		
		model.addAttribute("marketing", marketing);
		model.addAttribute("room", room);
		model.addAttribute("date", formdate.format(date));
    	return "indent/addIndent";
    }
	
	
	@RequestMapping("/addIndent.do")
    public String addIndent(String clientname,String paymenttype,Indent record,Integer roomid,Integer marketingid,Integer userid,Date createTime,Float accomplishMoney,Float receivableMoney,String clinetelephone){ 
	
		
	 //更改房间表状态
		Map<String,Object> map=new HashMap<String,Object>();
		map.put("id", roomid);
		map.put("state", "认购");
		int i = rs.updateByidstate(map);
		System.out.println("i="+i);
			
	//添加订单
		Date date=new Date();
		long title = date.getTime();
		record.setCreatetime(createTime);
		record.setRoomprice(accomplishMoney);
		record.setUserid(userid);
		record.setTitle(title+"");
		record.setMarketingid(marketingid);
		record.setState("认购");
		int insert = service.insert(record);
		System.out.println("insert="+insert);
		
		//更改机会表状态
		Map<String,Object> map1=new HashMap<String, Object>();
		map1.put("id",marketingid);
		map1.put("state","认购");
	    int im = ms.updateByidstate(map1);
	    System.out.println("im="+im);
	
		//添加房款信息
		Housepayment h=new Housepayment();
		
		Float uncollectedMoney=record.getRoomtotal()-accomplishMoney;
		h.setUserid(userid);
		h.setAccomplishmoney(accomplishMoney);
		h.setReceivablemoney(record.getRoomtotal());
		h.setUncollectedmoney(uncollectedMoney);
	    h.setMaketigid(marketingid);
	    h.setPaymenttype(paymenttype);
	    h.setFname(clientname);
	    System.out.println("clientname="+clientname);	    
		if(uncollectedMoney>0)
		{
		   h.setState("未还完");
		}else{
			h.setState("已还完");	
		}
		h.setCollectiontime(createTime);
	    h.setRoomid(roomid);
	    
	    Map<String,Object> map2 =new HashMap<String, Object>();
	    map2.put("userId",userid);
	    map2.put("MarketingId",marketingid);
	    map2.put("title",title+"");
	    
	    Indent indent2 = is.selectByuidmidct(map2); 
	   
	    h.setIndentid(indent2.getId());
	    h.setBargainid(null);
	    int is = hs.insert(h);
	    System.out.println("is="+is);
	    
	    System.out.println("clinetelephone+"+clinetelephone);
	    DingShiQi_01 duanxin =new DingShiQi_01();
	    duanxin.dingshiqi_01(clinetelephone);
	    
	    
    	return "redirect:/marketing/marketing.do";
    }
	
	@RequestMapping("/update.do")
    public String delete(Integer id,Model model){
		
	   //改变订单状态作废  
		Map<String,Object> map=new HashMap<String, Object>();
		map.put("state","作废");
		map.put("id",id);
		int i = is.updateByPrimaryKeystate(map);
		
		//改变房间状态
		Indent indent = is.selectByPrimaryKey(id);
		Integer marketingid = indent.getMarketingid();
		
		Marketing marketing = ms.selectByPrimaryKey(marketingid);
		
		Integer roomid = marketing.getRoomid();
		
		System.out.println("roomid:+"+roomid);
		Map<String,Object> map1=new HashMap<String,Object>();
		map1.put("id", roomid);
		map1.put("state", "已开盘");
		int k = rs.updateByidstate(map1);
	    System.out.println("改变房间状态:+"+k);
		
        //删除还款
		int j = hs.deleteByIndentid(id);
		
		
		//修改销售机会表
		Map<String,Object> map2=new HashMap<String, Object>();
		map2.put("state", "草稿");
		map2.put("id", marketingid);
		int l = ms.updateByidstate(map2);
		
		return "redirect:/indent/indent.do";
	}
		
	
	@RequestMapping("/toajax.do")
	public void toajax(Float accomplishMoney,Float receivableMoney,PrintWriter out,HttpServletRequest re) throws IOException{
		
		   float a=accomplishMoney/receivableMoney;
		   
		   if(a<=0.05)
		   {
			   out.print("定金不满足认购要求，需满足总额的5%");     
		   }else if(a>1){
			   out.print("定金已超过总额");    
		   }else{
			   out.print("定金满足认购要求");    
		   }
	}
	

	
	@RequestMapping("/qianyue.do")
    public String qianyue(Integer id,Model model){
		
		
		Indent indent = is.selectByPrimaryKey(id);
		System.out.println("marketingid"+indent.getMarketingid());
		Integer marketingid = indent.getMarketingid();
		Marketing marketing = ms.selectByPrimaryKey(marketingid);
		Integer clientid = marketing.getClientid();
		Client client = cs.selectOne(clientid);
		Integer userid = indent.getUserid();
		User user = us.selectByPrimaryKey(userid);
		Date date =new Date();
		
		SimpleDateFormat formdate = new SimpleDateFormat("yyyy-MM-dd");
		
		Integer roomid = marketing.getRoomid();
		Room room = rs.selectByPrimaryKey(roomid);
		
		model.addAttribute("client",client);
		model.addAttribute("room",room);
		model.addAttribute("user",user);
		model.addAttribute("date",formdate.format(date));
		model.addAttribute("indent",indent);
		model.addAttribute("maketing",indent.getMarketingid());	
		return "bargain/bargainadd";
	}
	
	
	@RequestMapping("daochu")
    public void daochu(Integer[] b,HttpServletRequest request,HttpServletResponse response) throws IOException{
		
		List<Indent> list=new ArrayList<Indent>();
		
		for(Integer l:b){
			
			Indent indent = is.selectByPrimaryKeyduo(l);
			list.add(indent);
		}
		
		response.setHeader("Content-Disposition","attachment;filename=indent.xls");//指定下载的文件名  
	    response.setContentType("application/vnd.ms-excel");  
		 Export1  ex = new Export1();   
		 ex.buildBaojiaExcel(list, "订单表.xls",response.getOutputStream());
		 
    }
	
	
	@InitBinder
	protected void initBinder(HttpServletRequest request,
			ServletRequestDataBinder binder) throws Exception {
		binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), true));
	}
}
